//
//  SceneDelegate.m
//  CreateWithSuper
//
//  Created by chenjunbo on 2024/5/18.
//

#import "SceneDelegate.h"
#import "CSCreateController.h"
#import "CSTutorialController.h"
#import "PPViewController.h"
#import "CSNetworkHelper.h"

#import "CSLoadController.h"
#import "CSGuidePopView.h"
#import "CSGuideConnectController.h"
#import "CSTutorialController.h"
#import "CSPolicyPopView.h"

@interface SceneDelegate ()

@end

@implementation SceneDelegate


- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
    
    UIWindowScene *windowScene = (UIWindowScene *)scene;

    self.window = [[UIWindow alloc] initWithWindowScene:windowScene];
    self.window.frame = windowScene.coordinateSpace.bounds;
    [self.window makeKeyAndVisible];
    
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Create" bundle:nil];
    CSLoadController *loadVC = [[CSLoadController alloc]init];

    if ([[NSUserDefaults standardUserDefaults]boolForKey:@"FirstInGuideConnect"]){
        [[CSNetworkHelper standardInstance]requestConfiguration];
        self.window.rootViewController = loadVC;
    }else{
        self.window.rootViewController = [PPViewController new];
    }
    
    [[CSNetworkHelper standardInstance]requestVps];
    [[CSNetworkHelper standardInstance]requestAiDefault];

}


- (void)sceneWillEnterForeground:(UIScene *)scene {
    
    //    || [OpenVManager sharedInstance].vpnStatus == NEVPNStatusInvalid
//    if ([JMADAboutOpen sharedInstance].isShowing || [JMADAboutInt sharedInstance].isShowing){
//        return;
//    }
    
    if ([self.window.rootViewController isKindOfClass:[UINavigationController class]]){
        
        UINavigationController *nav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
        
        for (UIViewController *child in nav.childViewControllers) {
            if (child == [nav visibleViewController]) {
                if ([child isKindOfClass:[CSGuidePopView class]] || 
                    [child isKindOfClass:[PPViewController class]] ||
                    [child isKindOfClass:[CSGuideConnectController class]] || 
                    [child isKindOfClass:[CSTutorialController class]] ||
                    [child isKindOfClass:[CSLoadController class]] ||
                    [child isKindOfClass:[CSPolicyPopView class]]) {
                    
                } else {
                    CSLoadController *newViewController = [[CSLoadController alloc]init];
                    newViewController.needPop = YES;
                    [nav pushViewController:newViewController animated:NO];

                }
            }
        }

    }
    
}

@end
